package demo3;
/**
 * 给数组排序，数组中的每个元素都是引用数据类型的对象。
 */
import java.util.Arrays;

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    public int compareTo(Student o) {
        //此方法用来比较两个对象的大小，分别返回负数，0和正数
        if(this.age - o.age > 0){
            return 1;
        } else if (this.age - o.age <0) {
            return -1;
        }else{
            return 0;
        }
        //重写的方法是根据年龄进行比较的
    }
}
public class Test {

    public static void main(String[] args) {
        Student[] students = {
                new Student("zhangsan",18),
                new Student("lisi",20)
        };
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }

}

